package com.ziqing.apeletnews.baseMvp;

import java.lang.ref.Reference;
import java.lang.ref.WeakReference;

/**
 * @author hwg
 * @date 2017/10/23
 */

public abstract class BasePresenter<V> {

    protected Reference<V> mViewRef ;

    protected V mView ;

    /**
     * 创建一个V的弱应用
     * @param view
     */
    public void attachView(V view){
        mViewRef = new WeakReference<V>(view) ;
    }

    /**
     * 返回V的引用对象
     * @return
     */
    public V getView(){
        return mViewRef.get() ;
    }

    /**
     * 判断V的引用是否为null(即V的引用是否有指向一个对象)
     * @return
     */
    public boolean isViewAttached(){
        return mViewRef != null && mViewRef.get() != null ;
    }

    /**
     * 清空V的引用
     */
    public void detachView(){
        if (mViewRef != null){
            mViewRef.clear();
            mViewRef = null ;
        }
    }

}
